package com.android.mangalib.activity;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.android.mangalib.adapter.TomeAdapter;
import com.android.mangalib.adapter.TomeAdapter.TomeViewHolder;
import com.android.mangalib.activity.R;
import com.android.mangalib.constant.Constant;
import com.android.mangalib.object.Tome;
import com.android.mangalib.parser.XMLParser;

/**
 * This activity is called when the user click on a collection in the main view
 */
public class CollectionActivity extends Activity {
	
	ListView tomeListView;
	List<Tome> collection;
	Button addButton;
	
    @Override
    /**
     * Main method, called when the activity is created
     */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collection);
        
        // Disable the "previous" button and remove the logo 
        getActionBar().setDisplayHomeAsUpEnabled(false);
        getActionBar().setDisplayShowHomeEnabled(false);
        
        // Get the title of the collection, where the user has clicked on the main activity
        final String title = getIntent().getStringExtra(Constant.COLLECTION_ACTIVITY);
        
        // Set the title of the activity as the title of the collection
        setTitle(title);
        
        tomeListView = (ListView)findViewById(R.id.tomeListView);
        try {
        	
        	/* Get the selected collection and create the adapter which created the view
        	 of the collection */
			collection = XMLParser.getAllTomesByCollection(this, title);
			TomeAdapter tomeAdapter = new TomeAdapter(this, collection);
			tomeListView.setAdapter(tomeAdapter);
			
			/**
	         * Manage the click on an item
	         * Creation of an anonymous class OnItemClickListener.
	         * Start a new activity which displays the details of the selected tome.
	         */
			tomeListView.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> listViewParam, View viewParam,
						int positionParam, long idParam) {
					Intent intent = new Intent(getApplicationContext(), TomeActivity.class);
					TomeViewHolder holder = (TomeViewHolder)(viewParam.getTag());
					// Send to the new activity the title of the collection and of the tome
					intent.putExtra(Constant.COLLECTION_ACTIVITY, title);
					intent.putExtra(Constant.TOME_ACTIVITY, holder.tvTitle.getText());
					startActivity(intent);
				}
	        	
	        });
			
			addButton = (Button) findViewById(R.id.addButton);
			addButton.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					Intent intent = new Intent(getApplicationContext(), AddTomeActivity.class);
					intent.putExtra(Constant.ADD_TOME_ACTIVITY, title);
					startActivity(intent);
				}
			});
			
        } catch (Exception e) {
        	Log.v("ERROR", e.getMessage());		}
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_collection, menu);
        return true;
    }

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
